package com.hxzy.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.net.HttpClient;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hxzy.shop.dto.req.LogisticsCompanyReq;
import com.hxzy.shop.dto.resp.ExpressLogisticsResp;
import com.hxzy.shop.dto.resp.QueryTrackDataResp;
import com.hxzy.shop.entity.LogisticsCompanyInformation;
import com.hxzy.shop.entity.OrderExpress;
import com.hxzy.shop.entity.OrderTable;
import com.hxzy.shop.mapper.FreightOrderMapper;
import com.hxzy.shop.mapper.LogisticsCompanyInformationMapper;
import com.hxzy.shop.mapper.OrderExpressMapper;
import com.hxzy.shop.service.ExpressLogisticsService;
import com.hxzy.shop.utils.logistics.ExpressOneHundredUtil;
import com.hxzy.shop.utils.logistics.HttpResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @Description 快递100物流查询
 * @Author hu-yf
 * @Data 2022/5/30 10:24
 */

@Service
public class ExpressLogisticsServiceImpl implements ExpressLogisticsService {

    @Resource
    private LogisticsCompanyInformationMapper logisticsCompanyInformationMapper;

    @Resource
    private FreightOrderMapper freightOrderMapper;

    @Resource
    private OrderExpressMapper orderExpressMapper;

    @Resource
    private RedisTemplate<String,String> redisTemplate;

     @Resource
    private ExpressOneHundredUtil expressOneHundredUtil;

    private String expressLogistics = "expressLogistics:";

  /**
     * @Description: 物流公司分页查询
     * @Param: [req]
     * @Return: void
     * @Author: hu-yf
     * @Date: 2022/5/30 13:11
     **/
    @Override
    public PageInfo<LogisticsCompanyInformation> getList(LogisticsCompanyReq req) {
        System.out.println(req);

        PageInfo<LogisticsCompanyInformation> pageInfo = PageHelper
                .startPage(req.getPageNum(), req.getPageSize())
                .doSelectPageInfo(() -> logisticsCompanyInformationMapper.getList(req.getKey()));
        return pageInfo;
    }

    /**
     * @Description: 查询物流信息
     * @Param: [logisticsOrderNo]
     * @Return: void
     * @Author: hu-yf
     * @Date: 2022/5/30 10:30
     **/
    public ExpressLogisticsResp get(String orderId){
        ExpressLogisticsResp expressLogisticsResp = new ExpressLogisticsResp();
        OrderTable orderTable = freightOrderMapper.selectById(orderId);
        if (orderTable != null && orderTable.getPickType()==3) {
            OrderExpress orderExpress = orderExpressMapper.selectOne(new QueryWrapper<OrderExpress>().eq("order_id", orderId));
            if (orderExpress != null && orderExpress.getCompanyCode()!=null && orderExpress.getLogisticsOrderNo()!=null) {
                String json = redisTemplate.opsForValue().get(expressLogistics + orderExpress.getLogisticsOrderNo());
                if (json != null) {
                    ExpressLogisticsResp expressLogisticsResp1 = JSON.parseObject(json, ExpressLogisticsResp.class);
                    return expressLogisticsResp1;
                } else {
                    HttpResult result = expressOneHundredUtil.doPost(orderExpress.getCompanyCode(), orderExpress.getLogisticsOrderNo());
                    if (result.getStatus() == HttpStatus.SC_OK && StringUtils.isNotBlank(result.getBody())){
                        JSONObject jsonObject = JSON.parseObject(result.getBody());
                        expressLogisticsResp.setLogisticsCompany(orderExpress.getLogisticsCompany());
                        expressLogisticsResp.setLogisticsOrderNo(orderExpress.getLogisticsOrderNo());
                        expressLogisticsResp.setData(JSON.parseArray(String.valueOf(jsonObject.get("data")), QueryTrackDataResp.class));
                        redisTemplate.opsForValue().set(expressLogistics+orderExpress.getLogisticsOrderNo(),JSON.toJSONString(expressLogisticsResp),30, TimeUnit.MINUTES);
                        return expressLogisticsResp;
                    }
                }
            }
        }
        return null;
    }
}
